import { useCounterStore } from '@/stores/counter'
const store = useCounterStore()
let webSock = null;
let connectState = false; // 标记 WebSocket 连接状态
let rec; //断线重连后，延迟5秒重新创建WebSocket连接  rec用来存储延迟请求的代码
let closeFlag = false; // 是否关闭socket
let global_callback = null;


let port = "8080"; // webSocket连接端口

function createWebSocket(callback) {
    const token = localStorage.getItem('token')
    // let wsUrl = `ws://192.168.1.14:7272?token=${token}&types=T`
    let wsUrl = `wss://api.xueqianban.vip/wss?token=${token}&types=T`
    console.log('token22222222222', token)
    console.log('createWebSocket', callback)
    if (webSock != null) {
        return
    }
    webSock = new WebSocket(wsUrl);
    global_callback = callback
    webSock.onopen = function () {
        // 连接打开时的处理
        websocketOpen();
    };

    webSock.onmessage = function (e) {
        // 接收消息时的处理
        websocketOnMessage(e);
    };
    webSock.onclose = function (e) {
        // 连接关闭时的处理
        websocketClose(e);
    };


    // 连接发生错误的回调方法
    webSock.onerror = function () {
        websocketError();
    };
}


//心跳设置
const heartCheck = {
    timeout: 30000, //每段时间发送一次心跳包 这里设置为20s
    timeoutObj: null, //延时发送消息对象（启动心跳新建这个对象，收到消息后重置对象）
    start: function () {
        this.timeoutObj = setInterval(function () {
            if (connectState) webSock.send(JSON.stringify({type: 0}));
        }, this.timeout);
    },

    reset: function () {
        clearTimeout(this.timeoutObj);
        this.start();
    }
};

//定义重连函数
let reConnect = () => {
    console.log("尝试重新连接", connectState);
    if (connectState) return; //如果已经连上就不在重连了
    rec && clearTimeout(rec);
    rec = setTimeout(function () { // 延迟5秒重连  避免过多次过频繁请求重连
        if (!connectState && localStorage.getItem('token')) {
            createWebSocket();
        }
    }, 5000);
};

// 实际调用的方法--发送消息
function sendSock(agentData) {
    console.log('发送消息sendSock', webSock)
    if (webSock.readyState === webSock.OPEN) {
        // 若是ws开启状态
        webSock.send(agentData)
    } else if (webSock.readyState === webSock.CONNECTING) {
        // 若是 正在开启状态，则等待1s后重新调用
        setTimeout(function () {
            sendSock(agentData);
        }, 1000);
    } else {
        // 若未开启 ，则等待1s后重新调用
        setTimeout(function () {
            sendSock(agentData);
        }, 1000);
    }
}

function websocketClose() {
    connectState = false;
    webSock = null;
    global_callback = null;
    closeFlag = false;
    store.disconnect()
    heartCheck.timeoutObj && clearTimeout(heartCheck.timeoutObj);
    // 清除重连定时器
    rec && clearTimeout(rec);
    // 执行重连
    reConnect();
}


// 数据接收
function websocketOnMessage(msg) {
    console.log('接收消息', msg)
    if (!msg || !msg.data) {
        // 可能得情况 - 心跳机制、无关信息接收
        console.log("收到数据：空消息");
        return;
    }

    // 收到信息为Blob类型时
    let result = JSON.parse(msg.data);
    if (result.type === 0 || result.type === '0') {
        //自己的业务
    } else {
        // global_callback(result)
        store.handleData(result)
    }

}

function closeSock({activeClose = false}) {
    closeFlag = activeClose;
    // 清除心跳定时器
    heartCheck.timeoutObj && clearTimeout(heartCheck.timeoutObj);
    // 清除重连定时器
    rec && clearTimeout(rec);
    if (closeFlag && webSock) {
        console.log("关闭socket");
        // 关闭socket
        webSock.close();
        store.disconnect()
    }
    // 初始化相关变量
    webSock = null;
    connectState = false;
}


const websocketError = () => {
    closeSock({activeClose: true});
    // 执行重连
    reConnect();
}

function websocketOpen(e) {
    connectState = true;
    store.connect()
    heartCheck.start(); //发送心跳 看个人项目需求
}
// 监听窗口事件，当窗口关闭时，主动断开websocket连接，防止连接没断开就关闭窗口，server端报错
window.onbeforeunload = function () {
    console.log('窗口关闭')
//    webSock.close()
}
export {sendSock, createWebSocket, closeSock};